package com.ftdigital.primemovers_operators.fragment;


import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.Paint;
import android.provider.MediaStore;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.adpter.OilAgreementListViewAdapter;
import com.ftdigital.primemovers_operators.adpter.TextListAdpter;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.model.AddOilHistory;
import com.ftdigital.primemovers_operators.model.AddOilInfo;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;
import com.google.gson.JsonSyntaxException;
import com.squareup.picasso.Picasso;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 信息确认
 */
@EFragment(R.layout.info_ensure_blank)
public class InfoEnsureFragment extends BaseFragment {
    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @FragmentArg("addOilInfo")
    AddOilInfo addOilInfo;

    @ViewById(R.id.licence_num_content)
    TextView licenceNum;

    @ViewById(R.id.truck_type_num_content)
    TextView licenceType;

    @ViewById(R.id.truck_company_content)
    TextView company;

    @ViewById(R.id.truck_des)
    TextView truckDes;

    @ViewById(R.id.listview)
    RecyclerView listView;


    @ViewById(R.id.truck_img)
    ImageView truckImg;


    OilAgreementListViewAdapter oilAgreementListViewAdapter;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_info_ensure));
        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        loadData();
    }

    private void loadData() {

        licenceNum.setText(addOilInfo.getData().getCar_num());
        licenceNum.getPaint().setColor(Color.BLUE);
        licenceNum.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        licenceType.setText(addOilInfo.getData().getCar_type());
        company.setText(addOilInfo.getData().getName());
        truckDes.setText(addOilInfo.getData().getDescs());
        Picasso.with(getActivity()).load(addOilInfo.getData().getIcon_url()).error(R.drawable.truck_icon).into(truckImg);

        oilAgreementListViewAdapter = new OilAgreementListViewAdapter(getActivity().getApplicationContext(), addOilInfo);
        listView.setAdapter(oilAgreementListViewAdapter);
//        try {
//            requestParams = "car_num=" + licence
//                    + "&gasstation_id=" + GlobalData.userInfo.getData().getGasstation_id()
//                    + "&supplier_id=" + GlobalData.userInfo.getData().getSupplier_id();
//            requestUrl = RequestAddr.addoilInfo + requestParams;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
//            @Override
//            public void onSuccess(Object object) {
//                super.onSuccess(object);
//                dealData(object);
//            }
//
//            @Override
//            public void onFailure(Throwable t, int errorNo, String strMsg) {
//                super.onFailure(t, errorNo, strMsg);
//            }
//        });
    }

//    AddOilInfo addOilInfo;
//    private void dealData(Object object) {
//        try {
//            addOilInfo = gson.fromJson(object.toString(), AddOilInfo.class);
//        } catch (JsonSyntaxException e) {
//            Toast.makeText(getActivity(), R.string.info_empty, Toast.LENGTH_SHORT).show();
//            return;
//        }
//
//        licenceNum.setText(addOilInfo.getData().getCar_num());
//        licenceType.setText(addOilInfo.getData().getCar_type());
//        company.setText(addOilInfo.getData().getName());
//        truckDes.setText(addOilInfo.getData().getDescs());
////        Log.i(GlobalData.TAG, "url:" + addOilInfo.getData().getIcon_url());
//        Picasso.with(getActivity()).load(addOilInfo.getData().getIcon_url()).error(R.drawable.truck_icon).into(truckImg);
//
//        oilAgreementListViewAdapter = new OilAgreementListViewAdapter(getActivity().getApplicationContext(), addOilInfo);
//        listView.setAdapter(oilAgreementListViewAdapter);
//
//    }

    @Click({R.id.back_btn, R.id.check_complate, R.id.submit, R.id.licence_num_content, R.id.truck_img})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
            case R.id.check_complate:
//                MainActivity.getInstance().switchFragment(this, WebviewFragment_.builder()
//                .infoUrl(addOilInfo.getData().getContract_url()));
//                Toast.makeText(getActivity(), addOilInfo.getData().getContract_url()+"", Toast.LENGTH_SHORT).show();
                break;
            case R.id.submit:
                MainActivity.getInstance().switchFragment(this, AddOilDetailFragment_.builder()
                .addOilInfo(addOilInfo)
                        .build());
                break;
            case R.id.licence_num_content:
                String[] driverArray = new String[addOilInfo.getData().getDriverList().size()];
                for (int i = 0; i < addOilInfo.getData().getDriverList().size(); i++) {
                    driverArray[i] = addOilInfo.getData().getDriverList().get(i).getName() +
                            "-" +
                            addOilInfo.getData().getDriverList().get(i).getMobile();
                }
                showSelect(driverArray);
                break;
            case R.id.truck_img:
                MainActivity.getInstance().switchFragment(this, TeamAddoilHistoryFragment_.builder()
                        .car_id(addOilInfo.getData().getCar_id())
                        .build());
                break;
        }
    }

    AlertDialog alertDialog;
    private void showSelect(String[] driverList) {
        alertDialog = new AlertDialog.Builder(MainActivity.getInstance()).create();
        alertDialog.show();

        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.listview_dialog);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        ListView listView = (ListView) window.findViewById(R.id.listView);
        TextView titleInfo = (TextView) window.findViewById(R.id.title_info);
        titleInfo.setText(R.string.title_driver_list);
        LinearLayout layout = (LinearLayout) window.findViewById(R.id.alert_layout);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });

        listView.setAdapter(new TextListAdpter(getActivity(), driverList));
//        listView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                alertDialog.dismiss();
//            }
//        });
    }
}
